package com.xinchao.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchao.cache.RedisCacheDao;
import com.xinchao.constant.CacheConstant;
import com.xinchao.constant.DateConstant;
import com.xinchao.dao.AdvertisementMonitorMapper;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.DeviceInfo;
import com.xinchao.model.dto.AdvertisementMonitorSumDTO;
import com.xinchao.model.vo.DataReportPointLocationVO;
import com.xinchao.model.vo.DataReportVO;
import com.xinchao.service.DataReportService;
import com.xinchao.service.DeviceInfoService;
import com.xinchao.util.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

/**
 * 数据报表Service实现类
 *
 * @author dxy
 * @date 2018/9/30 15:41
 */
@Service(value = "dataReportService")
public class DataReportServiceImpl implements DataReportService {
	@Autowired
	private AdvertisementMonitorMapper advertisementMonitorMapper;
	@Autowired
	private DeviceInfoService deviceInfoService;
	@Autowired
	private RedisCacheDao redisCacheDao;

	/**
	 * 获取总体数据VO列表
	 *
	 * @param advertisementName 广告名称
	 * @param startTime         开始时间
	 * @param endTime           结束时间
	 * @return List<DataReportVO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<DataReportVO> listTotalData(String advertisementName, String startTime, String endTime) throws ServiceException, ParseException {
		if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
			throw new ServiceException("开始时间或结束时间不能为空");
		}
		//返回报表列表
		List<DataReportVO> voList = new ArrayList<>();
		//解析时间段
		List<String> dayList = DateUtils.getDaysBetween(startTime, endTime, DateConstant.DATE_SHORT_ISO);
		Long startDate = null;
		Long endDate = null;
		for (String day : dayList) {
			Object cache = redisCacheDao.getCache(CacheConstant.CACHE_NAME_ADVERTISEMENT_MONITOR, day);
			startDate = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
			endDate = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MAX, DateConstant.LONG_DATE_FORMAT_STR);
			if (cache == null) {
				DataReportVO vo = getDataReportTotalDataVO(advertisementName, day, startDate, endDate);
				if (vo == null) {
					continue;
				}
				voList.add(vo);
			} else {
				JSONObject jsonObject = JSON.parseObject(cache.toString());
				if (jsonObject != null && jsonObject.size() > 0) {
					DataReportVO vo = new DataReportVO();
					vo.setTime(day);
					//广告名称不为空
					if (StringUtils.isNotBlank(advertisementName)) {
						String advertisementNameStr = jsonObject.getString(advertisementName);
						if (StringUtils.isNotBlank(advertisementNameStr)) {
							AdvertisementMonitorSumDTO sumDTO = JSON.parseObject(advertisementNameStr, AdvertisementMonitorSumDTO.class);
							//曝光次数
							Double exposuresSum = sumDTO.getExposuresSum();
							if (exposuresSum == null) {
								exposuresSum = 0D;
							}
							vo.setExposuresSum(exposuresSum);
							//触达人次
							Double touchSum = sumDTO.getTouchSum();
							if (touchSum == null) {
								touchSum = 0D;
							}
							vo.setTouchSum(touchSum);
							//观看人次
							Double watchSum = sumDTO.getWatchSum();
							if (watchSum == null) {
								watchSum = 0D;
							}
							vo.setWatchSum(watchSum);
							//观看时长
							Double playDurationSum = sumDTO.getPlayDurationSum();
							if (playDurationSum == null) {
								playDurationSum = 0D;
							}
							//观看时长精确到秒
							double duration = playDurationSum / 1000;
							String str = String.format("%.2f", duration);
							vo.setPlayDurationSum(Double.valueOf(str));
							//平均观看时长=总时长/观看人次
							if (watchSum == 0) {
								vo.setWatchAvg("0");
							} else {
								double watchAvg = duration / watchSum;
								vo.setWatchAvg(String.format("%.2f", watchAvg));
							}
							//观看率=观看人次/触达人次
							if (touchSum == 0) {
								vo.setWatchRatio("0");
							} else {
								double watchRatio = watchSum / touchSum;
								//如果watchRatio小于1
								if (watchRatio < 1) {
									String watchRatioFormat = String.format("%.2f", watchRatio);
									//去掉0.
									vo.setWatchRatio(watchRatioFormat.replaceAll("0.", ""));
								} else if (watchRatio > 1) {
									//如果watchRatio大于1
									String watchRatioFormat = String.format("%.2f", watchRatio);
									//去掉.
									vo.setWatchRatio(watchRatioFormat.replaceAll(".", ""));
								} else {
									//如果观看人次和触达人相等，也就是watchRatio=1.0时，观看率=100
									vo.setWatchRatio("100");
								}
							}
						}
					} else {
						Collection<Object> values = jsonObject.values();
						//曝光次数
						double exposures = 0;
						//触达人次
						double touch = 0;
						//观看人次
						double watch = 0;
						//观看时长
						double playDuration = 0;
						for (Object obj : values) {
							if (obj == null) {
								continue;
							}
							AdvertisementMonitorSumDTO dto = JSON.parseObject(obj.toString(), AdvertisementMonitorSumDTO.class);
							if (dto == null) {
								continue;
							}
							//曝光次数
							Double exposuresSum = dto.getExposuresSum();
							if (exposuresSum != null) {
								exposures = exposures + exposuresSum;
							}
							//触达人次
							Double touchSum = dto.getTouchSum();
							if (touchSum != null) {
								touch = touch + touchSum;
							}
							//观看人次
							Double watchSum = dto.getWatchSum();
							if (watchSum != null) {
								watch = watch + watchSum;
							}
							//观看时长精确到秒
							Double playDurationSum = dto.getPlayDurationSum();
							if (playDurationSum != null) {
								playDuration = playDuration + playDurationSum;
							}
						}
						//曝光次数
						vo.setExposuresSum(exposures);
						//触达人次
						vo.setTouchSum(touch);
						//观看人次
						vo.setWatchSum(watch);
						//观看时长精确到秒
						Double duration = playDuration / 1000;
						String str = String.format("%.2f", duration);
						vo.setPlayDurationSum(Double.valueOf(str));
						//平均观看时长=总时长/观看人次
						if (watch == 0) {
							vo.setWatchAvg("0");
						} else {
							double watchAvg = duration / watch;
							vo.setWatchAvg(String.format("%.2f", watchAvg));
						}
						//观看率=观看人次/触达人次
						if (touch == 0) {
							vo.setWatchRatio("0");
						} else {
							double watchRatio = watch / touch;
							//如果watchRatio小于1
							if (watchRatio < 1) {
								String watchRatioFormat = String.format("%.2f", watchRatio);
								//去掉0.
								vo.setWatchRatio(watchRatioFormat.replaceAll("0.", ""));
							} else if (watchRatio > 1) {
								//如果watchRatio大于1
								String watchRatioFormat = String.format("%.2f", watchRatio);
								//去掉.
								vo.setWatchRatio(watchRatioFormat.replaceAll(".", ""));
							} else {
								//如果观看人次和触达人相等，也就是watchRatio=1.0时，观看率=100
								vo.setWatchRatio("100");
							}
						}
					}
					voList.add(vo);
				}
			}
		}
		return voList;
	}

	/**
	 * 获取时段分析VO列表(结束时间默认同开始时间当天的最后一天是时间)
	 *
	 * @param advertisementName 广告名称
	 * @param startTime         开始时间
	 * @return List<DataReportTimeFrameAnalysisVO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<DataReportVO> listTimeFrameAnalysisData(String advertisementName, String startTime) throws ServiceException, ParseException {
		if (StringUtils.isBlank(startTime)) {
			throw new ServiceException("开始时间不能为空");
		}
		//返回列表
		List<DataReportVO> voList = new ArrayList<>();
		//从缓存中获取
		Object cache = redisCacheDao.getCache(CacheConstant.CACHE_NAME_TIME_FRAME_ANALYSIS, startTime);
		if (cache == null) {
			//开始日期
			Long startDate = DateUtils.stringTimeToLongTimeStamp(startTime, DateConstant.DATE_SHORT_ISO);
			//年月日
			String yearMonthDay = DateUtils.longTimeStampToStringDate(startDate, DateConstant.DATE_SHORT_ISO);
			//时间段列表
			List<String> timeFrameList = DateUtils.generateTimeFrameOfDay(startDate);
			Map<String, String> dayTimeFrameMap = DateConstant.DAY_TIME_FRAME_MAP;
			//结束日期
			Long endDate = null;
			for (int i = 0; i < timeFrameList.size(); i++) {
				String timeFrame = timeFrameList.get(i);
				//完整的结束日期
				endDate = DateUtils.stringTimeToLongTimeStamp(yearMonthDay + " " + timeFrame + ":00", DateConstant.LONG_DATE_FORMAT_STR);
				DataReportVO vo = getDataReportTotalDataVO(advertisementName, timeFrame, startDate, endDate);
				voList.add(vo);
				//重新计算开始日期
				String startDateStr = dayTimeFrameMap.get(timeFrame);
				startDate = DateUtils.stringTimeToLongTimeStamp(yearMonthDay + " " + startDateStr, DateConstant.LONG_DATE_FORMAT_STR);
			}
		} else {
			JSONObject jsonObject = JSON.parseObject(cache.toString());
			if (jsonObject != null && jsonObject.size() > 0) {
				//如果广告名称不为空
				if (StringUtils.isNotBlank(advertisementName)) {
					JSONArray jsonArray = jsonObject.getJSONArray(advertisementName);
					if (jsonArray != null && jsonArray.size() > 0) {
						for (int i = 0; i < jsonArray.size(); i++) {
							DataReportVO vo = jsonArray.getObject(i, DataReportVO.class);
							voList.add(vo);
						}
					}
				} else {
					//广告名称为空
					Collection<Object> values = jsonObject.values();
					//将同一时段放入同一个List中
					Map<String, List<DataReportVO>> voMap = new LinkedHashMap<>();
					//时间列表
					//统计统一时段的数据（如：6:30）
					for (Object obj : values) {
						if (obj == null) {
							continue;
						}
						JSONArray objects = JSON.parseArray(obj.toString());
						if (objects != null && objects.size() > 0) {
							for (int i = 0; i < objects.size(); i++) {
								DataReportVO object = objects.getObject(i, DataReportVO.class);
								String time = object.getTime();
								List<DataReportVO> dataList = voMap.get(time);
								if (CollectionUtils.isNotEmpty(dataList)) {
									dataList.add(object);
									voMap.put(time, dataList);
								} else {
									List<DataReportVO> list = new ArrayList<>();
									list.add(object);
									voMap.put(time, list);
								}
							}
						}
					}
					//计算同时段的数据
					for (Map.Entry<String, List<DataReportVO>> entry : voMap.entrySet()) {
						List<DataReportVO> valueList = entry.getValue();
						if (CollectionUtils.isNotEmpty(valueList)) {
							DataReportVO vo = new DataReportVO();
							vo.setTime(entry.getKey());
							//曝光次数
							double exposures = 0;
							//触达人次
							double touch = 0;
							//观看人次
							double watch = 0;
							//观看时长
							double playDuration = 0;
							for (int i = 0; i < valueList.size(); i++) {
								DataReportVO dataReportVO = valueList.get(i);
								if (dataReportVO == null) {
									continue;
								}
								//曝光次数
								Double exposuresSum = dataReportVO.getExposuresSum();
								if (exposuresSum != null) {
									exposures = exposures + exposuresSum;
								}
								//触达人次
								Double touchSum = dataReportVO.getTouchSum();
								if (touchSum != null) {
									touch = touch + touchSum;
								}
								//观看人次
								Double watchSum = dataReportVO.getWatchSum();
								if (watchSum != null) {
									watch = watch + watchSum;
								}
								//观看时长
								Double playDurationSum = dataReportVO.getPlayDurationSum();
								if (playDurationSum != null) {
									playDuration = playDuration + playDurationSum;
								}
							}
							//曝光次数
							vo.setExposuresSum(exposures);
							//触达人次
							vo.setTouchSum(touch);
							//观看人次
							vo.setWatchSum(watch);
							//观看时长精确到秒
							Double duration = playDuration / 1000;
							String str = String.format("%.2f", duration);
							vo.setPlayDurationSum(Double.valueOf(str));
							//平均观看时长=总时长/观看人次
							if (watch == 0) {
								vo.setWatchAvg("0");
							} else {
								double watchAvg = duration / watch;
								if (watchAvg == 1.0) {
									vo.setWatchAvg("100");
								} else {
									vo.setWatchAvg(String.format("%.2f", watchAvg));
								}
							}
							//观看率=观看人次/触达人次
							if (touch == 0) {
								vo.setWatchRatio("0");
							} else {
								double watchRatio = watch / touch;
								//如果watchRatio小于1
								if (watchRatio < 1) {
									String watchRatioFormat = String.format("%.2f", watchRatio);
									//去掉0.
									vo.setWatchRatio(watchRatioFormat.replaceAll("0.", ""));
								} else if (watchRatio > 1) {
									//如果watchRatio大于1
									String watchRatioFormat = String.format("%.2f", watchRatio);
									//去掉.
									vo.setWatchRatio(watchRatioFormat.replaceAll(".", ""));
								} else {
									//如果观看人次和触达人相等，也就是watchRatio=1.0时，观看率=100
									vo.setWatchRatio("100");
								}
							}
							voList.add(vo);
						}
					}
				}
			}
		}
		return voList;
	}

	/**
	 * 获取点位分析数据
	 *
	 * @param advertisementName 广告名称
	 * @param startTime         开始时间
	 * @param endTime           结束时间
	 * @param deviceNumber      机器码
	 * @return
	 */
	@Override
	public List<DataReportPointLocationVO> listDataReportPointLocationData(String advertisementName, String startTime, String endTime, String deviceNumber) throws ServiceException, ParseException {
		if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime) || StringUtils.isBlank(deviceNumber)) {
			throw new ServiceException("输入参数不能为空");
		}
		//检查终端是否存在
		DeviceInfo deviceInfo = deviceInfoService.getDeviceInfoByDeviceNumber(deviceNumber);
		if (deviceInfo == null) {
			throw new ServiceException("终端信息不存在，请联系管理员");
		}
		//返回列表
		List<DataReportPointLocationVO> voList = new ArrayList<>();
		//开始日期
		Long startDate = null;
		//结束日期
		Long endDate = null;
		//解析时间段
		List<String> dayList = DateUtils.getDaysBetween(startTime, endTime, DateConstant.DATE_SHORT_ISO);
		for (String day : dayList) {
			Object cache = redisCacheDao.getCache(CacheConstant.CACHE_NAME_POINT_LOCATION_ANALYSIS, day);
			//如果缓存中不存在，查询数据库
			if (cache == null) {
				startDate = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
				endDate = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MAX, DateConstant.LONG_DATE_FORMAT_STR);
				AdvertisementMonitorSumDTO dto = advertisementMonitorMapper.getAdvertisementMonitorSumByTime(advertisementName, startDate, endDate, deviceNumber);
				if (dto != null) {
					DataReportPointLocationVO vo = new DataReportPointLocationVO();
					//日期
					vo.setTime(day);
					//机器码
					vo.setDeviceNumber(deviceNumber);
					//城市
					vo.setCity(deviceInfo.getCity());
					//行政区域
					vo.setAdministrativeRegions(deviceInfo.getAdministrativeRegions());
					transforAdvertisementMonitorSumDTODataToDataReportPointLocationVO(vo, dto);
					voList.add(vo);
				}
			} else {
				JSONObject jsonObject = JSON.parseObject(cache.toString());
				JSONObject numberJsonObj = jsonObject.getJSONObject(deviceNumber);
				if (numberJsonObj != null && numberJsonObj.size() > 0) {
					DataReportPointLocationVO vo = new DataReportPointLocationVO();
					//日期
					vo.setTime(day);
					//机器码
					vo.setDeviceNumber(deviceNumber);
					//城市
					vo.setCity(deviceInfo.getCity());
					//行政区域
					vo.setAdministrativeRegions(deviceInfo.getAdministrativeRegions());
					//如果广告名称为空
					if (StringUtils.isBlank(advertisementName)) {
						Collection<Object> values = numberJsonObj.values();
						if (values != null && values.size() > 0) {
							//曝光次数
							double exposures = 0;
							//触达人次
							double touch = 0;
							//观看人次
							double watch = 0;
							//观看时长
							double playDuration = 0;
							for (Object obj : values) {
								AdvertisementMonitorSumDTO sumDTO = JSON.parseObject(obj.toString(), AdvertisementMonitorSumDTO.class);
								if (sumDTO == null) {
									continue;
								}
								//曝光次数
								Double exposuresSum = sumDTO.getExposuresSum();
								if (exposuresSum != null) {
									exposures = exposures + exposuresSum;
								}
								//触达人次
								Double touchSum = sumDTO.getTouchSum();
								if (touchSum != null) {
									touch = touch + touchSum;
								}
								//观看人次
								Double watchSum = sumDTO.getWatchSum();
								if (watchSum != null) {
									watch = watch + watchSum;
								}
								//观看时长
								Double playDurationSum = sumDTO.getPlayDurationSum();
								if (playDurationSum != null) {
									playDuration = playDuration + playDurationSum;
								}
							}
							//曝光次数
							vo.setExposuresSum(exposures);
							//触达人次
							vo.setTouchSum(touch);
							//观看人次
							vo.setWatchSum(watch);
							//观看时长精确到秒
							double duration = playDuration / 1000;
							String str = String.format("%.2f", duration);
							vo.setPlayDurationSum(Double.valueOf(str));
							//平均观看时长=总时长/观看人次
							if (watch == 0){
								vo.setWatchAvg("0");
							} else {
								double watchAvg = duration / watch;
								vo.setWatchAvg(String.format("%.2f", watchAvg));
							}
							//观看率=观看人次/触达人次
							if (touch == 0) {
								vo.setWatchRatio("0");
							} else {
								double watchRatio = watch / touch;
								//如果watchRatio小于1
								if (watchRatio < 1) {
									String watchRatioFormat = String.format("%.2f", watchRatio);
									//去掉0.
									vo.setWatchRatio(watchRatioFormat.replaceAll("0.", ""));
								} else if (watchRatio > 1) {
									//如果watchRatio大于1
									String watchRatioFormat = String.format("%.2f", watchRatio);
									//去掉.
									vo.setWatchRatio(watchRatioFormat.replaceAll(".", ""));
								} else {
									//如果观看人次和触达人相等，也就是watchRatio=1.0时，观看率=100
									vo.setWatchRatio("100");
								}
							}
						}
					} else {
						AdvertisementMonitorSumDTO dto = numberJsonObj.getObject(advertisementName, AdvertisementMonitorSumDTO.class);
						if (dto != null) {
							transforAdvertisementMonitorSumDTODataToDataReportPointLocationVO(vo, dto);
						}
					}
					voList.add(vo);
				}
			}
		}
		return voList;
	}

	/**
	 * 获取所有点位分析数据列表
	 * @return List<DataReportPointLocationVO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<DataReportPointLocationVO> listAllDataReportPointLocationData() throws ServiceException, ParseException {
		String advertisementName = "";
		//返回列表
		List<DataReportPointLocationVO> voList = new ArrayList<>();
		List<String> deviceNumberList = deviceInfoService.listAllDeviceNumber();
		for (String deviceNumber : deviceNumberList) {
			if (StringUtils.isBlank(deviceNumber)) {
				continue;
			}
			DeviceInfo deviceInfo = deviceInfoService.getDeviceInfoByDeviceNumber(deviceNumber);
			if (deviceInfo == null) {
				continue;
			}
			//开始日期
			Long startDate = null;
			//结束日期
			Long endDate = null;
			List<String> dayList = advertisementMonitorMapper.listAllDays();
			//解析时间段
			for (String day : dayList) {
				Object cache = redisCacheDao.getCache(CacheConstant.CACHE_NAME_POINT_LOCATION_ANALYSIS, day);
				//如果缓存中不存在，查询数据库
				if (cache == null) {
					startDate = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
					endDate = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MAX, DateConstant.LONG_DATE_FORMAT_STR);
					AdvertisementMonitorSumDTO dto = advertisementMonitorMapper.getAdvertisementMonitorSumByTime(advertisementName, startDate, endDate, deviceNumber);
					if (dto != null) {
						DataReportPointLocationVO vo = new DataReportPointLocationVO();
						//日期
						vo.setTime(day);
						//机器码
						vo.setDeviceNumber(deviceNumber);
						//城市
						vo.setCity(deviceInfo.getCity());
						//行政区域
						vo.setAdministrativeRegions(deviceInfo.getAdministrativeRegions());
						transforAdvertisementMonitorSumDTODataToDataReportPointLocationVO(vo, dto);
					}
				} else {
					JSONObject jsonObject = JSON.parseObject(cache.toString());
					JSONObject numberJsonObj = jsonObject.getJSONObject(deviceNumber);
					if (numberJsonObj != null && numberJsonObj.size() > 0) {
						DataReportPointLocationVO vo = new DataReportPointLocationVO();
						//日期
						vo.setTime(day);
						//机器码
						vo.setDeviceNumber(deviceNumber);
						//城市
						vo.setCity(deviceInfo.getCity());
						//行政区域
						vo.setAdministrativeRegions(deviceInfo.getAdministrativeRegions());
						//如果广告名称为空
						if (StringUtils.isBlank(advertisementName)) {
							Collection<Object> values = numberJsonObj.values();
							if (values != null && values.size() > 0) {
								//曝光次数
								double exposures = 0;
								//触达人次
								double touch = 0;
								//观看人次
								double watch = 0;
								//观看时长
								double playDuration = 0;
								for (Object obj : values) {
									AdvertisementMonitorSumDTO sumDTO = JSON.parseObject(obj.toString(), AdvertisementMonitorSumDTO.class);
									if (sumDTO == null) {
										continue;
									}
									//曝光次数
									Double exposuresSum = sumDTO.getExposuresSum();
									if (exposuresSum != null) {
										exposures = exposures + exposuresSum;
									}
									//触达人次
									Double touchSum = sumDTO.getTouchSum();
									if (touchSum != null) {
										touch = touch + touchSum;
									}
									//观看人次
									Double watchSum = sumDTO.getWatchSum();
									if (watchSum != null) {
										watch = watch + watchSum;
									}
									//观看时长
									Double playDurationSum = sumDTO.getPlayDurationSum();
									if (playDurationSum != null) {
										playDuration = playDuration + playDurationSum;
									}
								}
								//曝光次数
								vo.setExposuresSum(exposures);
								//触达人次
								vo.setTouchSum(touch);
								//观看人次
								vo.setWatchSum(watch);
								//观看时长精确到秒
								double duration = playDuration / 1000;
								String str = String.format("%.2f", duration);
								vo.setPlayDurationSum(Double.valueOf(str));
								//平均观看时长=总时长/观看人次
								if (watch == 0){
									vo.setWatchAvg("0");
								} else {
									double watchAvg = duration / watch;
									vo.setWatchAvg(String.format("%.2f", watchAvg));
								}
								//观看率=观看人次/触达人次
								if (touch == 0) {
									vo.setWatchRatio("0");
								} else {
									double watchRatio = watch / touch;
									//如果watchRatio小于1
									if (watchRatio < 1) {
										String watchRatioFormat = String.format("%.2f", watchRatio);
										//去掉0.
										vo.setWatchRatio(watchRatioFormat.replaceAll("0.", ""));
									} else if (watchRatio > 1) {
										//如果watchRatio大于1
										String watchRatioFormat = String.format("%.2f", watchRatio);
										//去掉.
										vo.setWatchRatio(watchRatioFormat.replaceAll(".", ""));
									} else {
										//如果观看人次和触达人相等，也就是watchRatio=1.0时，观看率=100
										vo.setWatchRatio("100");
									}
								}
							}
						} else {
							AdvertisementMonitorSumDTO dto = numberJsonObj.getObject(advertisementName, AdvertisementMonitorSumDTO.class);
							if (dto != null) {
								transforAdvertisementMonitorSumDTODataToDataReportPointLocationVO(vo, dto);
							}
						}
						voList.add(vo);
					}
				}
			}
		}
		return voList;
	}

	/**
	 * 将AdvertisementMonitorSumDTO数据转移到DataReportPointLocationVO中
	 * @param vo DataReportPointLocationVO
	 * @param dto AdvertisementMonitorSumDTO
	 */
	private void transforAdvertisementMonitorSumDTODataToDataReportPointLocationVO(DataReportPointLocationVO vo, AdvertisementMonitorSumDTO dto) {
		//曝光次数
		Double exposuresSum = dto.getExposuresSum();
		vo.setExposuresSum(exposuresSum);
		//触达人次
		Double touchSum = dto.getTouchSum();
		vo.setTouchSum(touchSum);
		//观看人次
		Double watchSum = dto.getWatchSum();
		vo.setWatchSum(watchSum);
		//观看时长精确到秒
		double duration = 0;
		Double playDurationSum = dto.getPlayDurationSum();
		if (playDurationSum == null ) {
			vo.setPlayDurationSum(null);
		} else {
			duration = playDurationSum / 1000;
			String str = String.format("%.2f", duration);
			vo.setPlayDurationSum(Double.valueOf(str));
		}
		//平均观看时长=总时长/观看人次
		if (watchSum == null || watchSum == 0){
			vo.setWatchAvg("0");
		} else {
			double watchAvg = duration / watchSum;
			vo.setWatchAvg(String.format("%.2f", watchAvg));
		}
		//观看率=观看人次/触达人次
		if ( watchSum == null || touchSum == null || touchSum == 0) {
			vo.setWatchRatio("0");
		} else {
			double watchRatio = watchSum / touchSum;
			//如果watchRatio小于1
			if (watchRatio < 1) {
				String watchRatioFormat = String.format("%.2f", watchRatio);
				//去掉0.
				vo.setWatchRatio(watchRatioFormat.replaceAll("0.", ""));
			} else if (watchRatio > 1) {
				//如果watchRatio大于1
				String watchRatioFormat = String.format("%.2f", watchRatio);
				//去掉.
				vo.setWatchRatio(watchRatioFormat.replaceAll(".", ""));
			} else {
				//如果观看人次和触达人相等，也就是watchRatio=1.0时，观看率=100
				vo.setWatchRatio("100");
			}
		}
	}

	/**
	 * 获取数据报表总数VO
	 *
	 * @param advertisementName 广告名称
	 * @param time              日期
	 * @param startTime         开始日期
	 * @param endTime           结束日期
	 * @return DataReportVO
	 */
	private DataReportVO getDataReportTotalDataVO(String advertisementName, String time, Long startTime, Long endTime) {
		AdvertisementMonitorSumDTO dto = advertisementMonitorMapper.getAdvertisementMonitorSumByTime(advertisementName, startTime, endTime, null);
		if (dto == null) {
			return null;
		}
		DataReportVO vo = new DataReportVO();
		//日期
		vo.setTime(time);
		//曝光次数
		Double exposuresSum = dto.getExposuresSum();
		vo.setExposuresSum(exposuresSum);
		//触达人次
		Double touchSum = dto.getTouchSum();
		vo.setTouchSum(touchSum);
		//观看人次
		Double watchSum = dto.getWatchSum();
		vo.setWatchSum(watchSum);
		//观看时长精确到秒
		double duration = 0;
		Double playDurationSum = dto.getPlayDurationSum();
		if (playDurationSum == null ) {
			vo.setPlayDurationSum(null);
		} else {
			duration = playDurationSum / 1000;
			String str = String.format("%.2f", duration);
			vo.setPlayDurationSum(Double.valueOf(str));
		}
		//平均观看时长=总时长/观看人次
		if (watchSum == null || watchSum == 0){
			vo.setWatchAvg("0");
		} else {
			double watchAvg = duration / watchSum;
			vo.setWatchAvg(String.format("%.2f", watchAvg));
		}
		//观看率=观看人次/触达人次
		if ( watchSum == null || touchSum == null || touchSum == 0) {
			vo.setWatchRatio("0");
		} else {
			double watchRatio = watchSum / touchSum;
			//如果watchRatio小于1
			if (watchRatio < 1) {
				String watchRatioFormat = String.format("%.2f", watchRatio);
				//去掉0.
				vo.setWatchRatio(watchRatioFormat.replaceAll("0.", ""));
			} else if (watchRatio > 1) {
				//如果watchRatio大于1
				String watchRatioFormat = String.format("%.2f", watchRatio);
				//去掉.
				vo.setWatchRatio(watchRatioFormat.replaceAll(".", ""));
			} else {
				//如果观看人次和触达人相等，也就是watchRatio=1.0时，观看率=100
				vo.setWatchRatio("100");
			}
		}
		return vo;
	}

}
